package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import commands.AddUserCommand;


/**
 * HomeServlet implementation class ServerServlet - the home page of the web interface
 * sing in and register
 * answer urls: the default
 */
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * constructor
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * urls: .../registry - give a registry page
	 *      / (the default page) - give a sign in page
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
          
        PrintWriter out = response.getWriter();
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title> clever dropbox =) </title>");
        out.println("</head>");
        out.println("<body bgcolor=#66CCFF>");
        
        //registry page
        if (request.getRequestURI().endsWith("/registry"))
        {
        	out.println("<h1 align='center'> registry </h1> <br> <form action='/registry' method='POST'>  " +
        			"<h3 align='center'> user name: <input type='text' name='userID' size='20'>  <br> <br>" +
            		"password:  <input type='password' name='password' size='20'> <br><br><br>" +
            		"<input type='submit' value='Submit'></h3></form><br><br><br><br>");
        	
        	out.println("<a href='/'> <h4 align='center'> to return to sign in page - click here... </h4></a>");
        
        }else
        //sign in page
        {
        	out.println("<h1 align='center'> sign in </h1> <br> <form action='/UserDropboxServlet' method='POST'>  " +
        		"<h3 align='center'> user name: <input type='text' name='userID' size='20'><br><br>" +
        		"password: <input type='password' name='password' size='20'> <br><br><br>" +
        		"<input type='submit' value='Submit'> </h3> </form><br><br><br><br>");
        	
        	out.println("<a href='/registry'> <h4 align='center'> Are you a new user? click here... </h4> </a>");    
        }
       
        out.println("</body>");
        out.println("</html>");
        out.close();
        
	}

	/**
	 * urls: .../registry - get the user and the password from the form and register the user
	 *      if there was a problem - tell it to the user
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String userID = request.getParameter("userID").toString();
        String password = request.getParameter("password").toString();

        int succeed = new AddUserCommand().action(userID, password);
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title> clever dropbox =) </title>");
        out.println("</head>");
        out.println("<body bgcolor=#66CCFF>");
        
        //the registry failed
        if ((succeed==0)&&(request.getRequestURI().endsWith("/registry")))
        {
        	out.println("<br><br><h3 align='center'> try to register again with another user name </h3>");
        	out.println("<a href='/registry'> <h3 align='center'> click here... </h3> </a>");
        }
        else
        {
        	//the page that the user see after registry
        	out.println("<br><br><h3 align='center'> <p>" + userID + ", Welcome to Clever Dropbox! =)</p> </h3>");
        	
        	out.println("<br> <a href='/DownloadServlet/dropBoxClient/dropBoxClient.rar'>  <h3 align='center'> now, you should install the dropbox on your computer - click here... </h3> </a>");
        	out.println("<br><br><a href=''>  <h3 align='center'> for sign in - click here... </h3> </a>");
        }
       
        out.println("</body>");
        out.println("</html>");

        out.close();

	}

}
